package org.jeecg.modules.fund.job;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.fund.entity.FundList;
import org.jeecg.modules.fund.entity.FundVal;
import org.jeecg.modules.fund.service.IFundListService;
import org.jeecg.modules.fund.service.IFundMovingService;
import org.jeecg.modules.fund.service.IFundValService;
import org.jeecg.modules.fund.util.FundDataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Deprecated
@Slf4j
@Component
public class FundEstimateValueJob {
    @Autowired
    private IFundListService fundListService;
    @Autowired
    private IFundValService fundValService;
    @Autowired
    private IFundMovingService fundMovingService;

    /**
     * 每周一至五，9点至15点，每三分钟执行一次，估值刷新任务
     * @throws InterruptedException
     */
   // @Scheduled(cron = "0 */3 9,10,11,13,14,15 * * MON-FRI")
    public void execute() throws InterruptedException {
        log.info("执行估值任务");
        // 获取自选基金列表
        List<FundList> fundLists = fundListService.list();
        for (FundList item : fundLists) {
            // 获取估算数据
            JSONObject object = FundDataUtil.getEstimateVal(item.getCode());
            // log.info("获取估值信息：{}", object);
            // 如果没有估算数据，这跳过
            if (object != null) {
                // 更新主表估值信息
                FundList fundListParam = new FundList();
                // 估值日期
                fundListParam.setEstimateTime(DateUtil.parseDate(object.getString("gztime")));
                // 估算值
                fundListParam.setEstimateVal(object.getBigDecimal("gsz"));
                // 估值率
                fundListParam.setEstimateRate(object.getBigDecimal("gszzl"));
                // 设置ID
                fundListParam.setId(item.getId());
                // 更新主表
                fundListService.updateById(fundListParam);

                // 构造实体
                FundVal entity = new FundVal();
                // 净值日期
                entity.setValDate(DateUtil.parseDate(object.getString("gztime")));
                // 所属基金
                entity.setFundId(item.getId());
                // 单位净值
                entity.setNowVal(object.getBigDecimal("gsz"));
                // 日增长率
                entity.setValRate(object.getBigDecimal("gszzl"));
                // 设置为估算数据
                entity.setIsEstimate(1);

                // 判断净值是否已经存在，如果存在则跳过
                FundVal model = fundValService.getOne(new LambdaQueryWrapper<FundVal>()
                        .eq(FundVal::getFundId, item.getId())
                        .eq(FundVal::getValDate, entity.getValDate()));
                // log.info("查询数据库中是否存在：{}", model);
                if (model != null) {
                    entity.setId(model.getId());
                }
                // log.info("净值：{}", model);
                // log.info("估算实体：{}", entity);
                // 判断是否是结算数据
                if (model == null || model.getIsEstimate() == 1) {
                    fundValService.saveOrUpdate(entity);
                    // 生成趋势线
                    fundValService.create(entity.getValDate(), "10", entity.getFundId());
                    fundValService.create(entity.getValDate(), "26", entity.getFundId());
                    fundValService.create(entity.getValDate(), "60", entity.getFundId());
                    fundValService.create(entity.getValDate(), "250", entity.getFundId());
                }
            }
        }
    }
}
